<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="7" :lg="11" :md="12" :sm="24">
            <a-form-item label="入库日期">
              <j-date
                placeholder="请选择开始日期"
                class="query-group-cust"
                v-model="queryParam.enterDate_begin"
              ></j-date>
              <span class="query-group-split-cust"></span>
              <j-date placeholder="请选择结束日期" class="query-group-cust" v-model="queryParam.enterDate_end"></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="单据编号">
              <a-input placeholder="请输入单据编号" v-model="queryParam.billNo"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="4" :lg="7" :md="8" :sm="24">
              <a-form-item label="型号编码">
                <j-input placeholder="请输入型号编码" v-model="queryParam.modelCode"></j-input>
              </a-form-item>
            </a-col>
            <a-col :xl="4" :lg="7" :md="8" :sm="24">
              <a-form-item label="单箱体积（m³）">
                <a-input placeholder="请输入单箱体积（m³）" v-model="queryParam.boxVolume"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="4" :lg="7" :md="8" :sm="24">
              <a-form-item label="仕向编码">
                <a-input placeholder="请输入仕向编码" v-model="queryParam.directionCode"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="7" :lg="11" :md="12" :sm="24">
              <a-form-item label="生产日期">
                <j-date
                  placeholder="请选择开始日期"
                  class="query-group-cust"
                  v-model="queryParam.manufactureDate_begin"
                ></j-date>
                <span class="query-group-split-cust"></span>
                <j-date
                  placeholder="请选择结束日期"
                  class="query-group-cust"
                  v-model="queryParam.manufactureDate_end"
                ></j-date>
              </a-form-item>
            </a-col>
            <a-col :xl="4" :lg="7" :md="8" :sm="24">
              <a-form-item label="库存数量">
                <a-input placeholder="请输入库存数量" v-model="queryParam.inventoryNum"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="4" :lg="7" :md="8" :sm="24">
              <a-form-item label="库存体积（m³）">
                <a-input placeholder="请输入库存体积（m³）" v-model="queryParam.inventoryVolume"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="4" :lg="7" :md="8" :sm="24">
              <a-form-item label="货品位置">
                <a-input placeholder="请输入货品位置" v-model="queryParam.goodsPosition"></a-input>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!-- <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button> -->
      <a-button type="primary" icon="download" @click="handleExportXls('完成品库存')">导出</a-button>
      <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel"
      >
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query
        :fieldList="superFieldList"
        ref="superQueryModal"
        @handleSuperQuery="handleSuperQuery"
      ></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="openBatchOutModal"><a-icon type="export" />出库</a-menu-item>
          <a-menu-item key="2" @click="batchDel" v-has="'endInventory:delBatch'"
            ><a-icon type="delete" />删除</a-menu-item
          >
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down"/></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width:80px;font-size: 12px;font-style: italic;"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">详情</a>
          <a-divider type="vertical" v-has="'endInventory:edit'" />
          <a @click="handleEdit(record)" v-has="'endInventory:edit'">编辑</a>
          <a-divider type="vertical" v-has="'endInventory:mobile'" />
          <a @click="handleMobile(record)" v-has="'endInventory:mobile'">移库</a>
          <a-divider type="vertical" v-has="'endInventory:del'" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)" v-has="'endInventory:del'"
            ><a>删除</a></a-popconfirm
          >
        </span>
      </a-table>

      <a-modal
        :title="'移库操作'"
        :visible="mobileModalVisible"
        @ok="submitMobile"
        @cancel="cancelMobile"
        :width="1000"
      >
        <a-form :model="mobileInventory">
          <a-form-item label="移库数量">
            <a-input v-model="mobileInventory.inventoryNum" type="number" placeholder="请输入移库数量"></a-input>
          </a-form-item>
          <a-form-item label="新货品位置">
            <a-input v-model="mobileInventory.goodsPosition" placeholder="请输入新货品位置"></a-input>
          </a-form-item>
        </a-form>
      </a-modal>
    </div>

    <a-modal :title="'出库'" :visible="batchOutParam.batchOutVisible" @ok="batchOut" @cancel="cancelOutBatchModal">
      <a-form :model="batchOutParam">
        <a-form-item label="出库日期" style="width: 250%;">
          <j-date v-model="batchOutParam.outDate" placeholder="请选择出库日期"></j-date>
        </a-form-item>
        <a-form-item label="单据号">
          <a-input v-model="batchOutParam.billNo" placeholder="请输入单据号"></a-input>
        </a-form-item>
      </a-form>
    </a-modal>

    <yw-end-finished-product-inventory-info-modal
      ref="modalForm"
      @ok="modalFormOk"
    ></yw-end-finished-product-inventory-info-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import YwEndFinishedProductInventoryInfoModal from './modules/YwEndFinishedProductInventoryInfoModal'
import { postAction } from '../../api/manage'

export default {
  name: 'YwEndFinishedProductInventoryInfoList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    YwEndFinishedProductInventoryInfoModal
  },
  data() {
    return {
      description: '完成品库存管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '入库日期',
          align: 'center',
          dataIndex: 'enterDate',
          customRender: function(text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          }
        },
        // {
        //   title: '单据编号',
        //   align: 'center',
        //   dataIndex: 'billNo'
        // },
        {
          title: '型号编码',
          align: 'center',
          dataIndex: 'modelCode'
        },
        {
          title: '仕向编码',
          align: 'center',
          dataIndex: 'directionCode'
        },
        {
          title: '库存数量',
          align: 'center',
          dataIndex: 'inventoryNum'
        },
        {
          title: '单箱体积（m³）',
          align: 'center',
          dataIndex: 'boxVolume'
        },
        {
          title: '库存体积（m³）',
          align: 'center',
          dataIndex: 'inventoryVolume'
        },
        {
          title: '货品位置',
          align: 'center',
          dataIndex: 'goodsPosition'
        },
        {
          title: '生产日期',
          align: 'center',
          dataIndex: 'manufactureDate',
          customRender: function(text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/ywEndFinishedProductInventory/ywEndFinishedProductInventoryInfo/list',
        delete: '/ywEndFinishedProductInventory/ywEndFinishedProductInventoryInfo/delete',
        deleteBatch: '/ywEndFinishedProductInventory/ywEndFinishedProductInventoryInfo/deleteBatch',
        exportXlsUrl: '/ywEndFinishedProductInventory/ywEndFinishedProductInventoryInfo/exportXls',
        importExcelUrl: 'ywEndFinishedProductInventory/ywEndFinishedProductInventoryInfo/importExcel',
        mobile: '/ywEndFinishedProductInventory/ywEndFinishedProductInventoryInfo/mobile',
        outBatch: '/ywEndFinishedProductInventory/ywEndFinishedProductInventoryInfo/outBatch'
      },
      dictOptions: {},
      superFieldList: [],
      mobileModalVisible: false,
      mobileInventory: {
        inventoryNum: '',
        goodsPosition: ''
      },
      batchOutParam: {
        batchOutVisible: false,
        ids: '',
        outDate: '',
        billNo: ''
      }
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    openBatchOutModal() {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择想要出库的库存后重试')
        return
      }

      this.batchOutParam.ids = this.selectedRowKeys.join(',')
      this.batchOutParam.batchOutVisible = true
    },
    batchOut() {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择想要出库的库存后重试')
        return
      }
      this.loading = true

      const param = new FormData()
      param.append('ids', this.batchOutParam.ids)
      param.append('outDate', this.batchOutParam.outDate)
      param.append('billNo', this.batchOutParam.billNo)
      postAction(this.url.outBatch, param)
        .then(response => {
          if (response.success) {
            this.$message.success(response.message)
            // 关闭模态框
            this.cancelOutBatchModal()
            // 取消选择
            this.onClearSelected()
            // 查询列表
            this.searchQuery()
          } else {
            this.$message.error(response.message)
          }
        })
        .finally(() => (this.loading = false))
    },
    cancelOutBatchModal() {
      this.batchOutParam.ids = ''
      this.batchOutParam.outDate = ''
      this.batchOutParam.billNo = ''
      this.batchOutParam.batchOutVisible = false
    },
    handleMobile(record) {
      // 为库存id赋值
      this.mobileInventory.inventoryId = record.id
      // 打开模态框
      this.mobileModalVisible = true
    },
    submitMobile() {
      // 封装提交参数
      const param = new FormData()
      param.append('inventoryId', this.mobileInventory.inventoryId)
      param.append('inventoryNum', this.mobileInventory.inventoryNum)
      param.append('goodsPosition', this.mobileInventory.goodsPosition)
      // 发起请求
      postAction(this.url.mobile, param)
        .then(response => {
          if (response.success) {
            this.$message.success(response.message)
            // 关闭模态框
            this.cancelMobile()
            // 查询列表
            this.searchQuery()
          } else {
            this.$message.error(response.message)
          }
        })
        .finally(() => (this.loading = false))
    },
    cancelMobile() {
      // 清空数据
      this.mobileInventory.inventoryId = ''
      this.mobileInventory.inventoryNum = ''
      this.mobileInventory.goodsPosition = ''
      // 关闭模态框
      this.mobileModalVisible = false
    },
    initDictConfig() {},
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'date', value: 'enterDate', text: '入库日期' })
      fieldList.push({ type: 'string', value: 'billNo', text: '单据编号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'modelCode', text: '型号编码', dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'boxVolume', text: '单箱体积（m³）', dictCode: '' })
      fieldList.push({ type: 'string', value: 'directionCode', text: '仕向编码', dictCode: '' })
      fieldList.push({ type: 'int', value: 'inventoryNum', text: '库存数量', dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'inventoryVolume', text: '库存体积（m³）', dictCode: '' })
      fieldList.push({ type: 'string', value: 'goodsPosition', text: '货品位置', dictCode: '' })
      fieldList.push({ type: 'date', value: 'manufactureDate', text: '生产日期' })
      this.superFieldList = fieldList
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
